import 'package:flutter/material.dart';

class SongSearchBar extends StatefulWidget {
  final Function(String) onSearch;

  const SongSearchBar({super.key, required this.onSearch});

  @override
  State<SongSearchBar> createState() => _SongSearchBarState();
}

class _SongSearchBarState extends State<SongSearchBar> {
  final TextEditingController _searchController = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  void _unfocusKeyboard() {
    _focusNode.unfocus();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // 背景点击区域（低优先级）
        Positioned.fill(
          child: IgnorePointer(
            child: GestureDetector(
              onTap: _unfocusKeyboard,
              behavior: HitTestBehavior.opaque,
            ),
          ),
        ),
        // 搜索栏（高优先级）
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: TextField(
            controller: _searchController,
            focusNode: _focusNode,
        decoration: InputDecoration(
          hintText: '搜索歌曲...',
          prefixIcon: const Icon(Icons.search),
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
          filled: true,
          fillColor: Colors.grey[800],
          contentPadding: const EdgeInsets.symmetric(vertical: 0),
        ),
        onSubmitted: (value) async {
          if (value.trim().isNotEmpty) {
            widget.onSearch(value);
          }
        },
          ),
        ),
      ],
    );
  }

  @override
  void dispose() {
    _unfocusKeyboard();
    _searchController.dispose();
    super.dispose();
  }
}